﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using QUT.Bio.SilverMap;
using QUT.Bio.SilverMap.ObjectModel;
using QUT.Bio.Graphs;
using QUT.Bio.Util;
using BlastExplorer.SilverMapDataProducer;

namespace BlastExplorer {
	public class SilverMapAdapter: DefaultSilverMapAdapter {

		#region Constructor

		/// <summary>
		/// Initialises the DefaultSilverMapAdapter.
		/// </summary>
		/// <param name="model">The Silvermap object model.</param>
		/// <param name="view">The SilverMap view.</param>
		/// <param name="overlay">A surface that can be used to display alerts and popup windows.</param>
		/// <param name="epsilon">The radius of the inner perimeter of the display torus.</param>
		/// <param name="sortPropertyNames">The names of properties used hitFeature rank nodes.</param>
		/// <param name="xrefCallback">A callback that is acivated when the user double clicks a sequence instance.</param>

		public SilverMapAdapter (
			MapControl map,
			PopupOverlay overlay,
			double epsilon,
			IEnumerable<string> sortPropertyNames
		)
			: base( map, overlay, epsilon, sortPropertyNames ) {
		}

		#endregion

		/// <summary>
		/// Gets a glyph to represent the node.
		/// </summary>
		/// <param name="node"></param>
		/// <returns></returns>

		public override FrameworkElement GetNodeShape ( Node<SequenceSummary> node ) {
			double W = DefaultSilverMapAdapter.NodeSize;
			double H = DefaultSilverMapAdapter.NodeSize;

			if ( node.Content.Sequence.Source == SequenceSource.Cdd ) {
				return new Rectangle {
					Fill = GetNodeColour( node ),
					Width = W,
					Height = H
				};
			}
			else if ( node.Content.Sequence.Source == SequenceSource.User ) {
				return new Polygon {
					Fill = GetNodeColour( node ),
					Width = DefaultSilverMapAdapter.NodeSize,
					Height = DefaultSilverMapAdapter.NodeSize,
					Points = new PointCollection( new Point[] { 
						new Point( 0.5 * W, 0.0 * H ),
						new Point( 1.0 * W, 1.0 * H ),
						new Point( 0.0 * W, 1.0 * H )
					} )
				};
			}
			else if ( ( (Sequence) node.Content.Sequence ).ContainsQuerySequence ) {
				Point [] points = new Point[5];
				
				for ( int i = 0; i < 5; i++ ) {
					double angle = Math.PI * ( i * 2.0 / 5 - 0.5 );
					double x = 0.5 * W * ( 0.5 + Math.Cos(angle) );
					double y = 0.5 * H * ( 0.5 + Math.Sin(angle) );
					points[i] = new Point( x, y );
				}
				
				Point [] reorderedPoints = new Point[5];

				reorderedPoints[0] = points[0];
				reorderedPoints[1] = points[2];
				reorderedPoints[2] = points[4];
				reorderedPoints[3] = points[1];
				reorderedPoints[4] = points[3];
				 
				
				return new Polygon {
					Fill = GetNodeColour( node ),
					Width = W * 1.0,
					Height = H * 1.0,
					Points = new PointCollection( reorderedPoints ),
					FillRule = FillRule.Nonzero
				};
			}
			else {
				return new Ellipse {
					Fill = GetNodeColour( node ),
					Width = W,
					Height = H
				};
			}
		}

		protected override void NodeEventHandler ( NodeView<SequenceSummary> nodeView, NodeAction action ) {
			if ( action == NodeAction.NodeDoubleClicked ) {
				Node<SequenceSummary> node = nodeView.Node;
				SequenceSummary sequenceSummary = (SequenceSummary) node.Content;
				ILinearDomain sequence = sequenceSummary.Sequence;

				if ( sequence.Source == SequenceSource.Cdd ) {
					return;
				}
			}

			base.NodeEventHandler( nodeView, action );
		}
	}
}
